﻿//
// TravModelView.cs:  Model-view subcomponent of the MPVC pattern
// This component provides change notifications and a minimal, read-only interface of the model to the view.
//
// Copyright 2011-2012 Kasey Osborn (Kasewick@Wordpress.com)
// Ms-PL license - Use and redistribute freely
//

using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;

using FileTreeSpace;
using ProgressSpace;
using TravPresenterAbstraction;


namespace TravModelViewSpace
{
    // Observer handlers:

    public delegate void AddressChangedEventHandler ();

    public delegate void FolderOpenedEventHandler (DirRoot root);
    public delegate void FolderClosedEventHandler ();
    public delegate void StatusChangedEventHandler ();

    public delegate void FolderCreatedEventHandler (string newPathName, string newLeafName);
    public delegate void FolderRenamedEventHandler (string oldPathName, string oldLeafName, string newLeafName);
    public delegate void FolderRemovedEventHandler (string folderName);

    public delegate void FileAddedEventHandler (string newBaseName, long newSize, string newHash, string newPathName, DateTime dateModified, FileAttributes newAttrib);
    public delegate void FileChangedEventHandler (string fullName, long newSize, FileAttributes newAttributes);
    public delegate void FileRenamedEventHandler (string oldFullName, string newFullName, string newBaseName);
    public delegate void FileRemovedEventHandler (string fullName);

    public delegate void DetailsInvalidatedEventHandler ();


    public abstract class TravModelView
    {
        private ITravPresenter presenter;
        public ITravPresenter Presenter
        {
            get { return presenter; }
            set
            {
                if (presenter != null)
                    throw new InvalidOperationException ("Application presenter already assigned");

                presenter = value;
            }
        }

        public abstract DetailModelView DetailView { get; }

        public string Arg { get; protected set; }
        public DirRoot Tree { get; protected set; }
        public string CurrentDir { get; protected set; }
        public string Status { get; protected set; }
        public string StatusFyi { get; protected set; }

        public string RootDir { get { return Tree.Name; } }
        public bool HasCurrentDir { get { return !String.IsNullOrEmpty (CurrentDir); } }

        public abstract bool IsBusy { get; }

        public DirPath CurrentDirPath
        {
            get
            {
                if (Tree == null || string.IsNullOrEmpty (CurrentDir))
                    return null;

                return new DirPath (Tree, CurrentDir);
            }
        }

        public event FolderOpenedEventHandler FolderOpened;
        public void OnFolderOpened (DirRoot root) { if (FolderOpened != null) FolderOpened (root); }

        public event FolderClosedEventHandler FolderClosed;
        public void OnFolderClosed () { if (FolderClosed != null) FolderClosed (); }

        public event AddressChangedEventHandler AddressChanged;
        public void OnAddressChanged () { if (AddressChanged != null) AddressChanged (); }

        public event FolderCreatedEventHandler FolderCreated;
        public void OnFolderCreated (string newPathName, string newLeafName)
        { if (FolderCreated != null) FolderCreated (newPathName, newLeafName); }

        public event FolderRenamedEventHandler FolderRenamed;
        public void OnFolderRenamed (string oldPathName, string oldLeafName, string newLeafName) { if (FolderRenamed != null) FolderRenamed (oldPathName, oldLeafName, newLeafName); }

        public event FolderRemovedEventHandler FolderRemoved;
        public void OnFolderRemoved (string folderName) { if (FolderRemoved != null) FolderRemoved (folderName); }

        public event StatusChangedEventHandler StatusChanged;
        public void OnStatusChanged () { if (StatusChanged != null) StatusChanged (); }
    }
}
